﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BUS;
using DTO;
namespace EC21_EDiamond.Admin
{
    public partial class QuanLyNguoiDung : System.Web.UI.Page
    {
        aspnet_UsersBUS userBus = new aspnet_UsersBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDanhSachUser();
            }
        }

        void LoadDanhSachUser()
        {
            grwDanhSachNguoiDung.DataSource = userBus.LoadDachSachUser();
            grwDanhSachNguoiDung.DataBind();
        }
        protected void btnDongY_Click(object sender, EventArgs e)
        {
            if (txtTaiKhoan.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Chưa Chọn Khách Hàng!');", true);
                return;
            }
            aspnet_UsersDTO user = new aspnet_UsersDTO();
            user.UserName = txtTaiKhoan.Text;
            aspnet_MembershipDTO member = new aspnet_MembershipDTO();
            member.Email = txtEmail.Text;
            if (userBus.CapNhatUser(user, member) != 0)
            {  
                if (drpQuyen.SelectedValue == "staff")
                {
                    //kiem tra user trong Roles Admin
                    //sau do cap nhat roles
                    string[] AdminRoles = Roles.GetUsersInRole("admin");
                    for (int i = 0; i < AdminRoles.Length; i++)
                    {
                        if (AdminRoles[i] == txtTaiKhoan.Text)
                        {
                            Roles.RemoveUserFromRole(txtTaiKhoan.Text, "admin");
                            Roles.AddUserToRole(txtTaiKhoan.Text, "staff");
                            break;
                        }
                    }
                }
                else
                {
                    //kiem tra user trong Roles Staff
                    //sau do cap nhat roles
                    string[] StaffRoles = Roles.GetUsersInRole("staff");
                    for (int i = 0; i < StaffRoles.Length; i++)
                    {
                        if (StaffRoles[i] == txtTaiKhoan.Text)
                        {
                            Roles.RemoveUserFromRole(txtTaiKhoan.Text, "staff");
                            Roles.AddUserToRole(txtTaiKhoan.Text, "admin");
                            break;
                        }
                    }
                }
                LoadDanhSachUser();
                txtTaiKhoan.Text = "";
                txtEmail.Text = "";
                txtTaiKhoan.ReadOnly = false;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thành Công!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thất Bại!');", true);
            }
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            if (Membership.DeleteUser(txtTaiKhoan.Text) == true)
            {
                txtTaiKhoan.Text = "";
                txtEmail.Text = "";
                txtTaiKhoan.ReadOnly = false;
                LoadDanhSachUser();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Xóa Thành Công!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Xóa Thất Bại!');", true);
            }
        }

        protected void grwDanhSachNguoiDung_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            txtTaiKhoan.ReadOnly = true;
            txtTaiKhoan.Text = grwDanhSachNguoiDung.Rows[e.NewSelectedIndex].Cells[1].Text;
            txtEmail.Text = grwDanhSachNguoiDung.Rows[e.NewSelectedIndex].Cells[2].Text;
            drpQuyen.SelectedValue = grwDanhSachNguoiDung.Rows[e.NewSelectedIndex].Cells[3].Text;
        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            aspnet_UsersBUS userBus= new aspnet_UsersBUS();
            grwDanhSachNguoiDung.DataSource = userBus.TimKiemNguoiDung(txtTaiKhoan.Text);
            grwDanhSachNguoiDung.DataBind();
        }
    }
}